/******************************************************************
* Filename:				CStateMachine.cpp
* Date Created:         04/29/2011       
* Mod. Date:            04/29/2011              
* Mod. Initials:		JW
* Author:				John Wileczek
* Purpose:				Define functions of the game state machine.
*						
*******************************************************************/
#include "CStateMachine.h"
#include "CAbstractState.h"

/*********************************************************************************
 * CStateMachine():		Constructor will call Initialize()   
 * 			      			      			      
 * Ins:					void 
 *      		     
 * Outs:				void
 *
 * Returns:				void
 *
 * Mod. Date:		    04/29/2011
 * Mod. Initials:	    JW
 ********************************************************************************/
CStateMachine::CStateMachine()
{
	Initialize();
}

/*********************************************************************************
 * ~CStateMachine():	Destructor will call Shutdown().  
 * 			      			      			      
 * Ins:					void 
 *      		     
 * Outs:				void
 *
 * Returns:				void
 *
 * Mod. Date:		    04/29/2011
 * Mod. Initials:	    JW
 ********************************************************************************/
CStateMachine::~CStateMachine()
{
	Shutdown();
}

/*********************************************************************************
 * Initialize():		This function will initialize the state machine and push   
 * 						the appropriate start state on.
 *			      
 * Ins:					void 
 *      		     
 * Outs:				void
 *
 * Returns:				void
 *
 * Mod. Date:		    04/29/2011  
 * Mod. Initials:	    JW  
 ********************************************************************************/
void CStateMachine::Initialize()
{
	//Initialize state machine with start state

}

/*********************************************************************************
 * Update():			This function will update the state machine and the correct.
 *						states in the vector
 *
 * Ins:					fDT 
 *      		     
 * Outs:				void
 *
 * Returns:				void
 *
 * Mod. Date:		    04/29/2011  
 * Mod. Initials:	    JW  
 ********************************************************************************/
void CStateMachine::Update(float fDT)
{
	if(m_vStates.size())
	{
		m_vStates[m_vStates.size() - 1]->Update(fDT);
	}
}

/*********************************************************************************
 * Shutdown():		This function will shutdown the state machine and clean up   
 * 					necessary memory.
 *			      
 * Ins:					void 
 *      		     
 * Outs:				void
 *
 * Returns:				void
 *
 * Mod. Date:		    04/29/2011  
 * Mod. Initials:	    JW  
 ********************************************************************************/
void CStateMachine::Shutdown()
{
	ClearAllStates();
}

/*********************************************************************************
 * Render():		Render the current state and objects in it.   
 * 			      			      
 * Ins:			    void  
 *      		     
 *
 * Outs:		    void  
 *
 * Returns:		    void  
 *
 * Mod. Date:		04/29/2011      
 * Mod. Initials:	JW      
 ********************************************************************************/
void CStateMachine::Render()
{
	//Render the last state may be changed to render all states
	if(m_vStates.size())
	{
		m_vStates[m_vStates.size() - 1]->Render();	
	}
}

/*********************************************************************************
 * PushState():		This function will push the new state onto the vector and   
 * 						enter correctly
 *			      
 * Ins:					pNewState 
 *      		     
 * Outs:				void
 *
 * Returns:				void
 *
 * Mod. Date:		    04/29/2011  
 * Mod. Initials:	    JW  
 ********************************************************************************/
void CStateMachine::PushState(CAbstractState *pNewState)
{
	m_vStates.push_back(pNewState);
	pNewState->Initialize();
}

/*********************************************************************************
 * PopState():			This function will pop the state at the back of the vector   
 * 						off and exit correctly.
 *			      
 * Ins:					void 
 *      		     
 * Outs:				void
 *
 * Returns:				void
 *
 * Mod. Date:		    04/29/2011  
 * Mod. Initials:	    JW  
 ********************************************************************************/
void CStateMachine::PopState()
{
	if(m_vStates.size())
	{
		m_vStates[m_vStates.size()-1]->Shutdown();
		m_vStates.pop_back();
	}
}

/*********************************************************************************
 * ClearAllStates():	This function will clear all the states and exit them  
 * 						correctly.
 *			      
 * Ins:					void 
 *      		     
 * Outs:				void
 *
 * Returns:				void
 *
 * Mod. Date:		    04/29/2011  
 * Mod. Initials:	    JW  
 ********************************************************************************/
void CStateMachine::ClearAllStates()
{
	for(unsigned int iState = 0; iState < m_vStates.size(); iState++)
	{
		m_vStates[iState]->Shutdown();
	}

	m_vStates.clear();
}